<template>
  <div>
    <el-dialog
      title="分班规则"
      :close-on-click-modal="false"
      @close="visible = false"
      width="600px"
      :visible.sync="visible"
    >
      <el-form
        :model="courseForm"
        :rules="courseRule"
        ref="courseForm"
        label-width="120px"
      >
        <el-form-item label="每班学生数量：" prop="studentNum">
          <el-input
            placeholder="请输入学生数量"
            v-model="courseForm.studentNum"
          ></el-input>
        </el-form-item>
        <p>注：优先将线下同一学校学生分配到同一班级</p>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取消</el-button>
        <el-button
          type="primary"
          @click="courseFormSubmit()"
          size="small"
          v-preventReClick
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { tableUpdate } from "@/api/api.js";
export default {
  data() {
    return {
      visible: false,
      courseForm: {
        studentNum: ""
      },
      id: "",
      courseRule: {
        studentNum: [
          { required: true, message: "请输入学生数量", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (!this.$valid.isInt(value)) {
                callback("请输入1-99999之间的整数"); //eslint-disable-line
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    init(row) {
      this.visible = true;
      this.id = row.id || 0;
      this.courseForm.studentNum = row.rule;
      this.$nextTick(() => {
        this.$refs["courseForm"].clearValidate();
      });
    },
    courseFormSubmit() {
      this.$refs["courseForm"].validate(valid => {
        if (valid) {
          this.submit();
        }
      });
    },
    async submit(e) {
      let res = await tableUpdate([
        {
          table_name: "nice_course",
          update: [
            {
              rule: this.courseForm.studentNum
            }
          ],
          where: [
            {
              id: this.id
            }
          ]
        }
      ]);
      if (res[0]) {
        return;
      }
      let data = res[1];
      if (data && data.code === 0) {
        this.$message({
          message: "编辑规则成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            this.visible = false;
            this.$emit("refreshDataList");
          }
        });
      } else {
        this.$message.error(data.msg);
      }
    }
  }
};
</script>
